<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="handleSearchList()" size="small">查询搜索</el-button>
        <el-button
                style="float:right;margin-right: 15px"
                @click="handleResetSearch()"
                size="small"
        >重置</el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="用户手机:">
            <el-input v-model="listQuery.phone" class="input-width" placeholder="用户手机"></el-input>
          </el-form-item>
          <el-form-item label="核销券码:">
            <el-input v-model="listQuery.code" class="input-width" placeholder="核销券码"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <div class="table-container">
      <el-table
        ref="deviceTable"
        :data="list"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="120" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="手机号" width="250" align="center">
            <template slot-scope="scope">{{scope.row.memberPhone}}</template>
        </el-table-column>
        <el-table-column label="团购券名称" width="200" align="center">
          <template slot-scope="scope">{{scope.row.certificateName}}</template>
      </el-table-column>
        <el-table-column label="团购券券码" width="200" align="center">
          <template slot-scope="scope">{{scope.row.sourceCode}}</template>
        </el-table-column>
        <el-table-column label="团购券状态" align="center">
          <template slot-scope="scope">{{scope.row.certificateStatus|formatStatus}}</template>
        </el-table-column>
        <el-table-column label="团购券来源" align="center">
          <template slot-scope="scope">{{scope.row.sourceType|formatType}}</template>
        </el-table-column>
        <el-table-column label="操作" width="240" align="center" v-if="mode==0">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import { fetchList, deleteCertificate } from "@/api/qsg/certificate";
import { updateDevice,deleteBind } from "@/api/qsg/device";
import { formatDate } from "@/utils/date";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 30
};
export default {
  name: "deviceList",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      listQuery1:{},
      list: null,
      meallist:null,
      total: null,
      listLoading: false,
      listLoading1:false,
      multipleSelection: [],
      mode:0, //默认列表进来,1为设备挑选套餐进来
      device:{},
      device1:{},
      multipleSelectionMeal:[]
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatStatus(certificateStatus) {
      if(certificateStatus==0){
        return '可使用'
      }else if (certificateStatus==1){
        return '已使用'
      }else {
        return '不可用'
      }
    },
    formatType(sourceType) {
      if (sourceType==0) {
        return "抖音";
      }else {
        return "美团";
      }
    }
  },
  methods: {
    listByDevice(){
      this.listLoading1 = true;
      listByDevice(this.listQuery1).then(response => {
        this.listLoading1 = false;
        this.meallist = response.data;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection)
    },
    handleSelectionMealChange(val) {
      this.multipleSelectionMeal = val;
      console.log(this.multipleSelectionMeal)
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleDelete(index, row) {
      this.$confirm("确认删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteCertificate(row.id).then(response => {
          this.$message({
            message: "删除成功",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      });
    },
    handleAdd() {
      this.$router.push({ path: "/drivermanage/addDevicemeal" });
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleBind() {
      let ids = '';
      for (let i=0; i<this.multipleSelection.length; i++) {
        ids = ids + this.multipleSelection[i].id + ','
      }
      ids = ids.substring(0,ids.lastIndexOf(","))
      this.device.mealId = ids;
      updateDevice(this.device.id, this.device).then(
              (response) => {
                this.$message({
                  message: "绑定成功",
                  type: "success",
                  duration: 1000,
                });
                this.$router.back();
              }
      );
      console.log(ids)
    },
    handleDeleteBind() {
      let ids = '';
      for (let i=0; i<this.multipleSelectionMeal.length; i++) {
        ids = ids + this.multipleSelectionMeal[i].id + ','
      }
      ids = ids.substring(0,ids.lastIndexOf(","))
      this.device1.mealId = ids;
      this.device1.id = this.device.id
      deleteBind(this.device1).then(
              (response) => {
                this.$message({
                  message: "解除绑定成功",
                  type: "success",
                  duration: 1000,
                });
                this.listByDevice();
              }
      );
      console.log(ids)
    },
    handleUpdate(index, row) {
      this.$router.push({
        path: "/drivermanage/updateDevicemeal",
        query: { id: row.id }
      });
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.records;
        this.total = response.data.total;
      });
    }
  }
};
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>


